package com.skyolder.milkbee.service;

import com.skyolder.milkbee.domain.Card;
import com.skyolder.milkbee.domain.Moderator;
import com.skyolder.milkbee.dto.HomeCard;
import com.skyolder.milkbee.dto.MonthGroupResult;
import com.skyolder.milkbee.repository.CardRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Service Implementation for managing Card.
 */
@Service
@Transactional
public class CardService {

    private final Logger log = LoggerFactory.getLogger(CardService.class);
    
    @Inject
    private CardRepository cardRepository;

    /**
     * Save a card.
     *
     * @param card the entity to save
     * @return the persisted entity
     */
    public Card save(Card card) {
        log.debug("Request to save Card : {}", card);
        Card result = cardRepository.save(card);
        return result;
    }

    /**
     *  Get all the cards.
     *  
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public Page<Card> findAll(Pageable pageable) {
        log.debug("Request to get all Cards");
        Page<Card> result = cardRepository.findAll(pageable);
        return result;
    }

    /**
     *  Get one card by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public Card findOne(Long id) {
        log.debug("Request to get Card : {}", id);
        Card card = cardRepository.findOne(id);
        return card;
    }

    /**
     *  Delete the  card by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Card : {}", id);
        cardRepository.delete(id);
    }
    @Transactional(readOnly = true) 
	public Page<Card> findByCategoryId(Long categoryId, Pageable pageable) {
		// TODO Auto-generated method stub
		return cardRepository.findByCategoryId(categoryId,pageable);
	}
    @Transactional(readOnly = true) 
	public List<HomeCard> doQueryCardRecommends(int count) {
		// TODO Auto-generated method stub
		Pageable pageable=new PageRequest(0,count);
		return cardRepository.doQueryCardRecommends(pageable);
	}
    @Transactional(readOnly = true) 
	public List<HomeCard> doQueryCardEssences(Integer count) {
		Pageable pageable=new PageRequest(0,count);
		return cardRepository.doQueryCardEssences(pageable);
	}
    @Transactional(readOnly = true) 
	public Page<Card> findByUserId(Long userId, Pageable pageable) {
		// TODO Auto-generated method stub
		return cardRepository.findByUserId(userId,pageable);
	}
    @Transactional(readOnly = true) 
	public Page<Card> doQueryAccusationByModerator(List<Moderator> moderators, Pageable pageable) {
		// TODO Auto-generated method stub
		List<Long> mid=new ArrayList<Long>();
		for(Moderator m:moderators){
			mid.add(m.getUserId());
		}
		return cardRepository.doQueryAccusationByModerator(mid,pageable);
	}
    @Transactional(readOnly = true) 
	public List<MonthGroupResult> doGroupByleaguersAndMonth(List<Long> leids) {
		// TODO Auto-generated method stub
		return cardRepository.doGroupByleaguersAndMonth(leids);
	}
    @Transactional(readOnly = true) 
	public List<MonthGroupResult> doGroupMonth(List<Long> leids, ZonedDateTime beginer, ZonedDateTime ender) {
		return cardRepository.doGroupMonth(leids,beginer,ender);
	}
    @Transactional(readOnly = true) 
	public Page<Card> doQueryApplyByModerator(List<Moderator> moderators, Pageable pageable) {
		// TODO Auto-generated method stub
		List<Long> mid=new ArrayList<Long>();
		for(Moderator m:moderators){
			mid.add(m.getUserId());
		}
		return cardRepository.doQueryApplyByModerator(mid,pageable);
	}
    @Transactional(readOnly = true) 
	public Page<Card> queryReport(Pageable pageable) {
		return cardRepository.queryReport(pageable);
	}
    @Transactional(readOnly = true) 
	public Page<Card> queryHome(Pageable pageable) {
		return cardRepository.queryHome(pageable);
	}
    @Transactional(readOnly = true) 
	public Page<Card> queryEssence(Pageable pageable) {
		return cardRepository.queryEssence(pageable);
	}
    @Transactional(readOnly = true) 
    public Page<Card> queryApplyEssence(Pageable pageable) {
    	return cardRepository.queryApplyEssence(pageable);
    }
    @Transactional(readOnly = true) 
    public Page<Card> queryDenyEssence(Pageable pageable) {
    	return cardRepository.queryDenyEssence(pageable);
    }
    @Transactional(readOnly = true) 
	public Page<Card> queryAttachment(Pageable pageable) {
		return cardRepository.queryAttachment(pageable);
	}

	public Page<Card> findByUserIdIn(List<Long> them, Pageable pageable) {
		return cardRepository.findByUserIdIn(them,pageable);
	}
}
